package close.mazegame.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import close.mazegame.MazeGame;
import close.mazegame.art.MiscArt;

public abstract class Screen {

	protected final String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ      0123456789.,:;'\"!?$%()-=+/";
	protected MazeGame mazeGame;
	protected SpriteBatch spriteBatch;
	protected BitmapFont font;
	
	public Screen(){
	}
	
	//Init and dispose will be called when a screen is switched to or from, use to create and end spritebatches and the like
	public void init(MazeGame mazegame){
		this.mazeGame = mazegame;
		font = new BitmapFont();
		spriteBatch = new SpriteBatch();
	}
	
	public void dispose(){
		font.dispose();
		spriteBatch.dispose();
	}
	
	protected void setScreen(Screen screen) {
		mazeGame.setScreen(screen);
	}
	
	public boolean isInCharSet(char c){
		return chars.contains(Character.toString(c).toUpperCase());
	}
	
	public void draw(TextureRegion region, float x, float y, float width, float height) {
		draw(region, x, y, width, height, 0.0f);
	}
	
	public void draw(TextureRegion region, float x, float y, float width, float height, float angle) {
		spriteBatch.draw(region, x, y, width/2.0f, height/2.0f, width, height, 1.0f, 1.0f, angle); //Assume rotation about the centre
	}
	
	public void drawString_Font(String string, float x, float y) {
		font.draw(spriteBatch, string, x, y);
	}
	
	public void drawString(String string, float x, float y, float scale){
		drawString(string, x,  y, scale, false, new Color(1, 1, 1, 1));
	}

	public void drawString(String string, float x, float y, float scale, boolean centre, Color color) {
		string = string.toUpperCase();
		int len = string.length();
		for (int i = 0; i < string.length(); i++) {
			int charIndex = chars.indexOf(string.charAt(i));
			if (charIndex >= 0) {
				Sprite s = new Sprite(MiscArt.chars[charIndex]);
				s.setColor(color);
				s.setSize(8 * scale, 8 * scale);
				s.setY(y);
				if (!centre) {
					s.setX(x + i * 8 * scale);
				} else {
					s.setX(x + (i - len / 2) * 8 * scale);
				}
				s.draw(spriteBatch);
			}
		}
	}
	
	public abstract void render();

	public abstract void update(float delta);

	public abstract boolean isOverlay();

	public void drawFPS() {
		spriteBatch.begin();
		this.drawString_Font(Integer.toString(Gdx.graphics.getFramesPerSecond()) + " FPS", 0, 20);
		spriteBatch.end();
	}

}
